package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}22:11
 */
public class E15 {
    public static void main(String[] args) {
        int[][] matrix = {{1},{8},{12}};
        for (int i : new E15().spiralOrder(matrix)){
            System.out.print(i+"\t");
        }
    }
    public int[] spiralOrder(int[][] matrix) {
        if (matrix.length==0){
            return new int[0];
        }
        //定义边界值
        int left = 0;
        int right = matrix[0].length-1;
        int top = 0;
        int bottom = matrix.length-1;
        int index = 0;

        int[] result = new int[matrix.length*matrix[0].length];

        while (true) {//判断之余还把该加的加了   每次遍历行列的时候 下一次行列的其实点和范围就会进行相应的改变
            //横向
            for (int i = left; i <= right;i++){
                result[index++] = matrix[top][i];

            }
            //向下之前要进行判断
            if (++top>bottom){
                break;
            }
            for (int i = top; i <= bottom; i++){
                result[index++] = matrix[i][right];

            }
            //向右之前进行是否有必要向右的判断
            if (left > --right){
                break;
            }
            for (int i = right; i>=left;i--){
                result[index++] = matrix[bottom][i];
            }
            //向上之前进行判断
            if (top > --bottom){
                break;
            }
            for(int i = bottom; i >= top;i--){
                result[index++] = matrix[i][left];

            }
            //之后又要向右进行判断
            if (++left>right){
                break;
            }
        }
        return result;
    }

}
